﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using System.Collections.Generic;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.ManageArtType
{
    public partial class AddSubType : System.Web.UI.Page
    {
       public int menuId = 0;
       public int typeId = 0;
       public bool showSub = true;
      public   ArtTypeBLL bll = new ArtTypeBLL();
        public IList<ArtType> list;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(Request["menuId"]))
            {
                menuId = int.Parse(Request["menuId"]);
            }
            if (!string.IsNullOrEmpty(Request["typeId"]))
            {
                typeId = int.Parse(Request["typeId"]);
            }
            this.BindType();
        }
        void BindType()
        {
            ;
            ArtType at ;
            if (menuId != 0)
            {
                at = bll.GetArtTypeById(bll.GetArtTypeById(menuId).pid);
                list = bll.GetArtTypeByPid(at.id,1);
                this.typeName.Text = at.typeName;

            }
            else 
            {
                at = bll.GetArtTypeById(typeId);
                list = bll.GetArtTypeByPid(at.id,1);
                this.typeName.Text = at.typeName;

            }
            if (GetMenuCount(at) >=2)
            {
                showSub = false;
            }
            this.hdfPid.Value = at.id.ToString();
          
          
        }int i = 0;
        public int GetMenuCount(ArtType at)
        {

            if (at.pid != 0)
            {
                i++;
                ArtType att = bll.GetArtTypeById(at.pid);
                return  GetMenuCount(att);
            }
            else {
                return i;
            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string typeName = this.txtTypeName.Text.Trim();
            int pid = int.Parse(this.hdfPid.Value);
            if (string.IsNullOrEmpty(typeName))
            {
                StringUtil.Alert("请填写分类名","-1");
            }
            ArtType at = new ArtType();
            at.pid = pid;
            at.typeName = typeName;
            at.isShow = 1;
            at.orderId = int.Parse(this.txtOrderId.Text); 
            bll.Add(at);
            this.BindType();
            string menu = bll.GetTypeTree();
            FileUtil.WriteToFile(Server.MapPath("/manage/menutext/menu.txt"), menu);
            
        }

    }
}
